package com.axjy.web;

import com.axjy.pojo.User;
import com.axjy.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by asus on 2018/1/10.
 */
@Controller
@RequestMapping("/user")
public class UserHandler {
    @Resource
    private UserService userService;
    @GetMapping("/list")
    public String list(Integer userRole, HttpServletRequest req){
        List<User> list=userService.search(userRole);
        req.setAttribute("list",list);
        return "user/list";
    }
    @GetMapping("/card")
    public String toCard(){
        return "user/card";
    }
    @GetMapping("/check")
    public String check(){
        return "user/check";
    }
    @GetMapping(value="/checkCode")
    @ResponseBody
    public String checkCode(String userCode){
        String msg=userService.checkCode(userCode);
        return msg;
    }
    @GetMapping(value="/detail/{id}")
    @ResponseBody
    public User detail(@PathVariable Integer id){
        User user=  userService.get(id);
        return user;
    }
    @GetMapping("/add")
    public String add(){
        return "user/add";
    }

    @PostMapping("/add")
    public String add(User user,HttpSession session){
        userService.add(user,session);
        return "redirect:/user/list";
    }
}
